<template>
  <div>
    <el-card class="box-card">
      <el-row>
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/main/personalInvestment' }">用户管理</el-breadcrumb-item>
          <el-breadcrumb-item>企业担保用户</el-breadcrumb-item>
        </el-breadcrumb>
      </el-row>
      <el-row><br><br></el-row>
      <el-row>
        <el-col :span="1"><br></el-col>
        <el-col :span="20">
          <el-form :inline="true" :model="dto" :rules="dtoRules" class="form-inline" ref="dtoForm">
            <el-form-item prop="name">
              <el-input v-model="dto.name" placeholder="用户名/企业名称"></el-input>
            </el-form-item>
            <el-form-item>
              <el-date-picker
              v-model="timeValue"
              type="daterange"
              range-separator="至"
              start-placeholder="请选择注册时间"
              end-placeholder="请选择注册时间"
              value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
            </el-form-item>
            <el-form-item prop="openstate">
              <el-select v-model="dto.openstate" clearable placeholder="请选择开户状态">
                <el-option label="已开户" value="已开户"></el-option>
                <el-option label="未开户" value="未开户"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="invest">
              <div style="display: flex;">
                <el-input v-model="dto.invest.startInvest" placeholder="担保总额起值(元)" style="flex: 1;"></el-input>
                <span style="margin: 0 8px;">-</span>
                <el-input v-model="dto.invest.endInvest" placeholder="担保总额终值(元)" style="flex: 1;"></el-input>
              </div>
            </el-form-item>
            <el-form-item prop="uncompleted">
              <div style="display: flex;">
                <el-input v-model="dto.uncompleted.startUncompleted" placeholder="待还担保额起值(元)" style="flex: 1;"></el-input>
                <span style="margin: 0 8px;">-</span>
                <el-input v-model="dto.uncompleted.endUncompleted" placeholder="待还担保额起值(元)" style="flex: 1;"></el-input>
              </div>
            </el-form-item>
          </el-form>         
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="1"><br></el-col>
        <el-col :span="15">
          <el-button type="primary" icon="el-icon-search" @click="getUserDetailsList('dtoForm')">搜索</el-button>
          <el-button type="warning" @click="resetForm('dtoForm')">重置</el-button>
        </el-col>
      </el-row>
      <el-row><br><br></el-row>
      <el-row>
        <el-col :span="1"><br></el-col>
        <el-col :span="22">
          <el-table
            :data="userDetailsList"
            style="width: 100%">
            <el-table-column
              label="序号"
              type="index"
              width="50">
            </el-table-column>
            <el-table-column
              prop="uname"
              label="用户名"
              width="90">
            </el-table-column>
            <el-table-column
              prop="firmname"
              label="企业名称"
              width="90">
            </el-table-column>
            <el-table-column
              prop="legalperson"
              label="法人代表"
              width="80">
            </el-table-column>
            <el-table-column
              prop="phone"
              label="法人手机号"
              width="120">
            </el-table-column>
            <el-table-column
              prop="openstate"
              label="开户状态"
              width="80">
            </el-table-column>
            <el-table-column
              prop="time"
              label="注册时间"
              width="100"
              :formatter="formatDateToYMD">
            </el-table-column>
            <el-table-column
              prop="investmoney"
              label="担保总额(元)"
              width="110">
            </el-table-column>
            <el-table-column
              prop="uncompletedmoney"
              label="待还担保总额(元)"
              width="130">
            </el-table-column>
            <el-table-column
              prop="status"
              label="状态"
              width="80">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.type == 1" type="success">正常</el-tag>
                <el-tag v-else type="danger">锁定</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope"> 
                <el-button
                  size="mini" v-if="scope.row.type == 1" type="warning"
                  @click="handleDisable(scope.row)">锁定</el-button>
                <el-button
                  size="mini" v-if="scope.row.type == 0" type="success"
                  @click="handleEnable(scope.row)">解锁</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="1"><br></el-col>
        <el-col :span="5">
          <el-pagination
            @current-change="handleCurrentChange"
            :current-page.sync="dto.page"
            :page-size="dto.size"
            layout="total, prev, pager, next"
            :total="total">
          </el-pagination>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    var checkInvest = (rule, value, callback) => {
      let start = value.startInvest
      let end = value.endInvest
      if (start == '' && end == '') {
        callback()
      }
      const regex = /^\d{1,10}(?:\.\d{1,2})?$/;
      if (start != '' && !regex.test(start.toString())){
        callback(new Error('起始：请输入正数且小数点后最多两位'));
      }
      if (end !='' && !regex.test(end.toString())){
        callback(new Error('终止：请输入正数且小数点后最多两位'));
      }
      if (start != '' && end != "" && start >= end) {
        callback(new Error('起值不能大于终值'));
      }
      callback()
    };
    var checkUncompleted = (rule, value, callback) => {
      let start = value.startUncompleted
      let end = value.endUncompleted
      if (start == '' && end == '') {
        callback()
      }
      const regex = /^\d{1,10}(?:\.\d{1,2})?$/;
      if (start != '' && !regex.test(start.toString())){
        callback(new Error('起始：请输入正数且小数点后最多两位'));
      }
      if (end !='' && !regex.test(end.toString())){
        callback(new Error('终止：请输入正数且小数点后最多两位'));
      }
      if (start != '' && end != "" && start >= end) {
        callback(new Error('起值不能大于终值'));
      }
      callback()
    };
    return {
      dto:{
        name:'',
        startTime:'',
        endTime:'',
        openstate:'',
        page:1,
        size:5,
        invest:{
          startInvest:'',
          endInvest:''
        },
        uncompleted:{
          startUncompleted:'',
          endUncompleted:''
        },
        userType:'custom_企业担保用户'
      },
      timeValue: '',
      userDetailsList:[],
      total:0,
      dtoRules:{
        invest:[
          {validator: checkInvest, trigger: 'blur'},
        ],
        uncompleted:[
          {validator: checkUncompleted, trigger: 'blur'},
        ]
      }
    }
  },
  mounted(){
    this.getUserDetailsList('dtoForm')
  },
  methods:{
    handleCurrentChange(val){
      this.dto.page = val
      this.getUserDetailsList()
    },
    //重置查询表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.timeValue = ''
      this.dto = {
        name:'',
        startTime:'',
        endTime:'',
        openstate:'',      
        page:1,
        size:5,
        invest:{
          startInvest:'',
          endInvest:''
        },
        uncompleted:{
          startUncompleted:'',
          endUncompleted:''
        },
        userType:'custom_企业担保用户'
      }
    },
    formatDateToYMD(row, column, value) {
      // 如果value是有效的日期时间字符串，可以使用以下方式直接格式化
      if (value) {
        const date = new Date(value);
        const year = date.getFullYear();
        const month = this.padZero(date.getMonth() + 1); // 月份是从0开始的，所以加1
        const day = this.padZero(date.getDate());
        const hours = this.padZero(date.getHours());
        const minutes = this.padZero(date.getMinutes());
        const seconds = this.padZero(date.getSeconds());
        return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
      }
      return '';
    },
    padZero(num) {
      return num < 10 ? '0' + num : num; // 用于补零操作，保证日期格式为两位数
    },
    getUserDetailsList(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let data = {
        name:this.dto.name,
        startTime:this.timeValue[0],
        endTime:this.timeValue[1],
        openstate:this.dto.openstate,
        startInvest:this.dto.invest.startInvest,
        endInvest:this.dto.invest.endInvest,
        startUncompleted:this.dto.uncompleted.startUncompleted,
        endUncompleted:this.dto.uncompleted.endUncompleted,
        page:1,
        size:5,
        userType:'custom_企业担保用户'
      }
      this.$axios.get("/userDetails/getUserDetailsList",{
        params:data
      }).then(res=>{
        if (res.data.code == 200) {
          this.userDetailsList = res.data.data.data
          this.total = res.data.data.total
        }
      })
        } else {
          return false
        }
      }); 
    },
    handleEnable(row){
      this.$axios.put("/userDetails/enableAccount/" + row.uid).then(res=>{
        if(res.data.code == 200){
          this.$message({
            message: '解锁成功！',
            type: 'success'
          });
          this.getUserDetailsList('dtoForm')
        }
      })
    },
    handleDisable(row){
      console.log(row)
      this.$axios.put("/userDetails/disableAccount/" + row.uid).then(res=>{
        if(res.data.code == 200){
          this.$message({
            message: '锁定成功！',
            type: 'success'
          });
          this.getUserDetailsList('dtoForm')
        }
      })
    },
  }
}
</script>

<style scoped>
.box-card {
  margin-left:5vh;
  margin-right:2vh;
  margin-top: 3vh;
  height: 88vh;
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>